package validarLogin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import negocio.Empresa;
import util.ObjectHelper;
import util.ServletHelper;

import com.google.api.services.plus.model.Person;

import dao.RepositorioEmpresa;

/**
 * Servlet implementation class ServletValidarLoginEmpresa
 */
@WebServlet(name = "ServletValidarLoginEmpresa", urlPatterns = { "/loginEmpresa" })
public class ServletValidarLoginEmpresa extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ServletValidarLoginEmpresa() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		
		String googleLogin = request.getParameter("google");
		String proximaPagina = "";
		
		if(ObjectHelper.isNull(googleLogin)){
			System.out.println(request.getParameter("inputEmailEmpresa"));
			System.out.println(request.getParameter("inputPasswordEmpresa"));
			
			
			RepositorioEmpresa rep = new RepositorioEmpresa();
			//		SQLi
			/*		Empresa user = rep.validarLoginEmpresa(
				request.getParameter("inputEmailEmpresa"),
				request.getParameter("inputPasswordEmpresa"));
			 */		
			Empresa user = rep.validarLoginEmpresaSegura(request.getParameter("inputEmailEmpresa"),	request.getParameter("inputPasswordEmpresa"));
			
			
			if (user != null) {
				HttpSession session = request.getSession();
				session.setAttribute("usuarioEmpresa", user);
				proximaPagina = "cadastrarEmpresa.jsp";
			} else {
				request.setAttribute("mensagem", "Usuario nao logado");
				proximaPagina = "loginEmpresa.html";
			}
			
		}else{
			Empresa user = new Empresa(1, "Google", "Google", "Google Av", "google", "google");
			HttpSession session = request.getSession();
			session.setAttribute("usuarioEmpresa", user);
			proximaPagina = "cadastrarEmpresa.jsp";
		}
		
		ServletHelper.forward(request, response, proximaPagina);
	}

}
